package org.androidpn.server.console.controller;

import java.util.List;

import javax.print.attribute.standard.Severity;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.androidpn.server.entity.Room;
import org.androidpn.server.service.RoomService;
import org.androidpn.server.service.ServiceLocator;
import org.androidpn.server.util.Page;
import org.androidpn.server.util.Pageable;
import org.androidpn.server.xmpp.push.RoomManager;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * Controller 在线房间
 * 
 * @author 蔡琪
 * 
 */
public class RoomController extends MultiActionController {

	private RoomService roomService;
	private RoomManager roomManager;
	private int pageSize = 10;

	public RoomController() {

		roomService = (RoomService) ServiceLocator
				.getService(ServiceLocator.ROOM_SERVICE);
		roomManager = new RoomManager();
	}

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();

		String s = request.getParameter("s");
		int pageNumber = ServletRequestUtils.getIntParameter(request,
				"pageNumber", 1);
		Pageable pageable = new Pageable(pageNumber, pageSize);
		Page<Room> page = roomService.findPage(pageable);
		mav.addObject("s", s);
		mav.addObject("page", page);
		mav.setViewName("room/list");
		return mav;
	}

	/**
	 * 关闭游戏房间
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView closeRoom(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Long id = ServletRequestUtils.getLongParameter(request, "id");
		Room room = roomService.find(id);
		// 给客户端发送关闭房间指令
		roomManager.sendCloseRoomInstruct(room);
		roomService.closeRoom(id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:room.do");
		return mav;
	}
}
